package com.restfinder.common.component.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.restfinder.common.component.ErrorMessage;
import com.restfinder.common.component.JsonResponse;
import com.restfinder.common.component.ValidationJsonResponse;

public class JsonSpringUtils {
	
	public static String JSON_STATUS_SUCCESS	=	"SUCCESS";
	public static String JSON_STATUS_FAILURE	=	"FAILURE";

	public static void populateJsonByErrors(BindingResult result, ValidationJsonResponse json) {
		List<FieldError> allErrors = result.getFieldErrors();
		List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
		for (FieldError objectError : allErrors) {
			errorMesages.add(new ErrorMessage(objectError.getField(), objectError.getCode()));
		}
		json.setErrorMessageList(errorMesages);
	}
	
	public static boolean isSuccessResponse(JsonResponse jsonResponse){
		return jsonResponse.getStatus().equalsIgnoreCase(JSON_STATUS_SUCCESS);
	}
}
